package com.springboot.admin.service.impl;

import com.springboot.admin.entity.CommentsDto;
import com.springboot.admin.entity.PagesDto;
import com.springboot.admin.mapper.PagesDao;
import com.springboot.admin.service.PagesSerice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.*;

@Service
@Slf4j
public class PagesSericeImpl implements PagesSerice {

  @Autowired
  PagesDao pagesDao;

  @Autowired
  RedisTemplate redisTemplate;

  public List<PagesDto> getRecomList(String type) {

    List<PagesDto> list;

    if (type.equals("30")){
      list =  pagesDao.getRecomListBy30();
    } else if(type.equals("365")) {
      list = pagesDao.getRecomListBy365();
    } else {
      return pagesDao.getRecomList();
    }

    return list;
  }

  public int getAllCount() {
    int count = pagesDao.getAllCount();
    return count;
  }

  public int getCountViews() {
    int count = pagesDao.getCountViews();
    return count;
  }

  public List<CommentsDto> getAllComment() {
    return pagesDao.getAllComment();
  }

  public List<CommentsDto> getLatestComments() {
    return pagesDao.getLatestComments();
  }

  public Map<String,Object> getListByPage(String page){
    // pageHelper
    //  PageHelper.startPage(pageNo,5);
    //  List<PagesDto> list = pagesSerice.getAll();
    //  PageInfo<PagesDto> pagesDtoPageInfo = new PageInfo<>(list);
    //  resultMap.put("result", pagesDtoPageInfo.getList());
    //  resultMap.put("total", pagesDtoPageInfo.getTotal());

    Map resultMap= new HashMap();
    int pageNo = Integer.parseInt(page);
    resultMap.put("result", pagesDao.getAll(pageNo));
    resultMap.put("total", pagesDao.getAllCount());
    return resultMap;
  };

  public Object getListByCatePage(String cate,String page){
    Map<String, Object> resultMap = new HashMap<>();
    List<PagesDto> list = new ArrayList<>();
    int count = 0;
    count = pagesDao.getCountByCate(Integer.parseInt(cate));
    list = pagesDao.getListByCate(Integer.parseInt(cate), Integer.parseInt(page));
    resultMap.put("total", count);
    resultMap.put("result", list);
    return resultMap;
  };

  public Object getListByTagsPage(String cate,String page) throws UnsupportedEncodingException {
    Map<String, Object> resultMap = new HashMap<>();
    List<PagesDto> list = new ArrayList<>();
    int count = 0;
    count = pagesDao.getCountByTags(URLDecoder.decode(cate, "utf-8"));
    list = pagesDao.getListByTags(URLDecoder.decode(cate, "UTF-8"), Integer.parseInt(page));
    resultMap.put("total", count);
    resultMap.put("result", list);
    return resultMap;
  };

  public Object getCateList(){
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("result", pagesDao.getCate());
    return resultMap;
  };

  public Object getTagsList(){
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("result", pagesDao.getTags());
    return resultMap;
  };

  public Object getCommentById(String id){
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("result", pagesDao.getComment(id));
    return resultMap;
  };

  public Object getDetailByPageId(String id){
    // 如果不想使用redis，可使用查数据库代码，然后注释redisTemplate部分65-69行代码
    // PagesDto pagesDto  = pagesSerice.getDetail(Integer.parseInt(id));
    PagesDto pagesDto;
    try {
      // 默认从缓存取，如果缓存没有，从数据库查询，同时存入redis
      pagesDto = (PagesDto)redisTemplate.opsForValue().get(id);
      if (pagesDto == null){
        if (id.indexOf("-") > 0){
          // 如果是uuid则通过Uuid查询
          pagesDto = pagesDao.getDetailByUUID(id);
        } else {
          pagesDto = pagesDao.getDetail(Integer.parseInt(id));
        }
        //  添加版权信息
        pagesDto.setContent(pagesDto.getContent() + "<p style='opacity:0'><a href='https://www.dsiab.com' style='cursor:unset'>本文章由javascript技术分享原创和收集</a></p>");
        redisTemplate.opsForValue().set(id,pagesDto);
      }
    } catch (Exception e){
      //  如果未配置redis，则默认查数据库
      pagesDto = pagesDao.getDetail(Integer.parseInt(id));
      pagesDto.setContent(pagesDto.getContent() + "<p style='opacity:0'><a href='https://www.dsiab.com' style='cursor:unset'>本文章由javascript技术分享原创和收集</a></p>");
    }

    // 浏览量加1
    pagesDao.updateViews(pagesDto);
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("result", pagesDto);
    return resultMap;
  };

}
